package com.demo.webapp.action.root;

import java.util.List;
import java.util.Map;
import java.util.Set;

import com.demo.core.model.Function;
import com.demo.core.model.Site;
import com.demo.core.model.SiteFunction;
import com.demo.core.service.base.FunctionService;
import com.demo.core.service.base.SiteFunctionService;
import com.demo.core.service.base.SiteService;
import com.demo.core.util.collection.JsonUtils;
import com.demo.webapp.action.PageAction;

/**
 * 站点功能Action
 * 当前站点的功能操作
 * @author ryuu.kk
 *
 */
public class SiteFunctionAction extends PageAction {

	/**
	 * 版本序列化ID
	 */
	private static final long serialVersionUID = -8332054592988418411L;
	
	/**
	 * 主键ID
	 */
	private Long id;
	
	/**
	 * 站点ID
	 */
	private Long siteId;
	
	/**
	 * json字符串
	 */
	private String json;
	
	/**
	 * 功能模型(树结构)
	 */
	private SiteFunction tree;

	/**
	 * 站点功能模型列表
	 */
	private List<SiteFunction> siteFunctionList;
	
	/**
	 * 站点功能模型
	 */
	private SiteFunction siteFunction;
	
	/**
	 * 站点列表
	 */
	private List<Site> siteList;
	
	/**
	 * 站点功能模型服务
	 */
	private SiteFunctionService siteFunctionService;

	/**
	 * 站点服务
	 */
	private SiteService siteService;
	
	/**
	 * 站点功能服务
	 */
	private FunctionService functionService;
	
	/**
	 * 功能模型
	 */
	public String execute() {
		
		if (id == null) {
			//id = Constants.FUNCTION_ROOT_ID;
			this.tree = siteFunctionService.queryFunctionTreeRoot(siteId);
		} else {
			this.tree = siteFunctionService.queryFunctionTree(this.id, siteId);
		}
		
		
		// 本站点分配的站点功能(List)
		this.siteFunctionList = siteFunctionService.query(siteId);
		
		return SUCCESS;
	}
	
	/**
	 * 站点功能分配-一览
	 * @return String
	 */
	public String view() {
		
		// 定义站点
		Site site = null;
		// 取得所有站点信息
		this.siteList = siteService.findAll();
		
		if (siteId == null && siteList.size() > 0) {
			// 取得第一个站点作为默认站
			site = siteList.get(0);
			siteId = site.getId();
		} else {
			site = siteService.find(siteId);
		}
		
		if (site != null) {
			Long siteGroupId = site.getSiteGroupId();
			// 获取站点类型的所有功能树
			Function tree = functionService.tree(null, siteGroupId);
			// 得到站点已分配的功能树
			Map<Long, SiteFunction> sfMap = siteFunctionService.querySiteFunctionMap(site.getId(), Boolean.TRUE);
			
			Set<Long> keySet = sfMap.keySet();
			// 生成json树
			json = JsonUtils.zTree(tree, true, keySet.toArray());
		}
		return SUCCESS;
	}
	
	/**
	 * 站点功能分配-权限分配
	 * @return String
	 */
	public String authority() {
		String status = "RWD";
		//Set<Long> upSet = new HashSet<Long>();
		//Set<Long> delSet = new HashSet<Long>();
		if (siteId != null) {
			// 回收站点可用标志
			siteFunctionService.updateAll(siteId, status, false);
			if (siteFunctionList != null) {
				for (SiteFunction sf : siteFunctionList) {
					// 更新状态
					siteFunctionService.update(siteId, sf.getFunctionId(), status, true);
				}
			}
			addActionMessage("站点功能已成功分配.");
		}
		return NONE;
	}
	
	/**
	 * 创建站点功能信息
	 * @return String
	 */
	public String enable() {
		// 取得站点ID
		if (siteFunctionList != null) {
			for (SiteFunction sf : siteFunctionList) {
				siteFunctionService.update(sf.getId(), "RWD", sf.isEnabled());
			}
		}
		return CALLBACK;
	}
	
	//~~ settings here ---------------------------------------------------------------------
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public List<SiteFunction> getSiteFunctionList() {
		return siteFunctionList;
	}

	public void setSiteFunctionList(List<SiteFunction> siteFunctionList) {
		this.siteFunctionList = siteFunctionList;
	}

	public SiteFunction getSiteFunction() {
		return siteFunction;
	}

	public SiteFunction getTree() {
		return tree;
	}

	public void setSiteFunction(SiteFunction siteFunction) {
		this.siteFunction = siteFunction;
	}

	public void setSiteId(Long siteId) {
		this.siteId = siteId;
	}

	public Long getSiteId() {
		return siteId;
	}
	
	public List<Site> getSiteList() {
		return siteList;
	}
	
	public String getJson() {
		return json;
	}

	public void setSiteFunctionService(SiteFunctionService siteFunctionService) {
		this.siteFunctionService = siteFunctionService;
	}
	
	public void setFunctionService(FunctionService functionService) {
		this.functionService = functionService;
	}
	
	public void setSiteService(SiteService siteService) {
		this.siteService = siteService;
	}
}
